package cn.leonis.newcoder;

/**
 * @Author Tobi
 * @Date 2021-12-13 下午5:04
 * 生成数列求和
 */
public class Base01 {

    public static void main(String[] args) throws Exception {
/*        long sum = 0;
        long base = 9; //以9为基数
        for(int i = 0; i < 10; i++){ //一共十个数字
            sum += base; //累加数列
            base = base * 10 + 9; //当前数列乘10加9就是下一个数列
            System.out.println(base);
        }
        System.out.println(sum);*/

/*        String a = "9";
        long sum = 0;
        for(int i = 0;i<10;i++){
            sum = Long.parseLong(a) + sum;
            a = a + "9";
        }
        System.out.println(sum);*/
        String str = "hellonowcoder";
        int len = str.length()/3;
        String[] temp = new String[len + 1];
        int index = len;
        for (int i = str.length(); i > 0; i = i - 3) {
            temp[index--] = str.substring(i - 3 < 0 ? 0 : i - 3, i);
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i <= len; i++) {
            sb.append(temp[i]);
            sb.append(",");
        }
        String result = sb.toString();
        System.out.println(result.substring(0, result.length() - 1));
    }

    // 归并排序
    public int[] MySort (int[] arr) {
        // write code here
        if (arr.length < 2) {
            return arr;
        }
        int[] result = new int[arr.length];

        sort(arr, 0, arr.length - 1, result);

        return result;
    }

    public void sort(int[] arr, int left, int right, int[] result) {
        if (left < right) {
            int mid = (left + right) / 2;
            sort(arr, left, mid, result);
            sort(arr, mid + 1, right, result);
            merge(arr, left, mid, right, result);
        }
    }

    public void merge(int[] arr, int left, int mid, int right, int[] result) {
        int i = left;
        int j = mid + 1;
        int t = 0;
        while (i <= mid && j <= right) {
            if (arr[i] < arr[j]) {
                result[t++] = arr[i++];
            } else {
                result[t++] = arr[j++];
            }
        }

        while (i <= mid) {
            result[t++] = arr[i++];
        }

        while(j <= right) {
            result[t++] = arr[j++];
        }

        t = 0;

        while(left <= right) {
            arr[left++] = result[t++];
        }

    }
}
